<?php
require_once 'client'.EXT;

class Document extends Client{

	function __construct()
	{
		parent::__construct();
		$this->load->model('document_model');
	}
	
	function index($p=1)
	{
		$sql	=	'';
		if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['name']) && $_GET['name'] != '')
		{
			//$sql	=	"d.name LIKE '%".$_GET['name']."%'";
			$_SESSION['sql']	=	"d.name LIKE '%".$_GET['name']."%' AND d.cat=".$_GET['select'];
		}
		
		if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['name']) && $_GET['name'] == '')
		{
			$_SESSION['sql']	=	"cat = ".$_GET['select'];
		}
		
		if(isset($_SESSION['sql']))$sql	=	$_SESSION['sql'];
		//$p	=	(isset($_GET['p']) ? $_GET['p'] : 1);
		//echo $sql;
		$limit	=	5;
		$offset	=	($p-1)*$limit;	
		$result	=	$this->document_model->get_my_documents($_SESSION['_user']['user_id'],$limit,$offset,$sql);
		$this->data['documents']	=	$result['rows'];
		$conf	=	array(
			'base_url'		=>	base_url().'document',
			'cur_page'		=>	$p,	
			'total_rows'	=>	$result['num_rows'],
			'per_page'		=>	$limit
		);
		$this->pagination->initialize($conf);
		$this->data['pagnav']	=	$this->pagination->display();
		$this->data['current']		=	'document';
		$this->data['tpl_file']		=	'document/index';
		$this->load->view('layout/default', $this->data);
	}
	
	function upload()
	{
		$this->data['module_title']	=	'Upload document';
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			$this->load->helper('upload');
			$path = dirname($_SERVER['SCRIPT_FILENAME']).'/uploads/';
			$path	.=	$_SESSION['_user']['user_id'];
			//echo $path;
			create_dir($path);
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'doc|pdf|xls|docx|jpg|png|gif|jpeg|ico';
			//$name_file	=	$_FILES['']
			$explanation	=	$this->input->post('Explanation');
	
			$this->load->library('upload', $config);
			
	
			if (!$this->upload->do_upload('file'))
			{
				$error =$this->upload->display_errors();
				$this->data['error']	=	$error;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data('file'));
				//print_r($data);die;
				//print_r($data);die;
				$old_path	=	$data["upload_data"]["full_path"];
				
				$pathinfo	=	pathinfo($old_path);
				$name		=	$pathinfo["filename"];
				$ext		=	$pathinfo["extension"];
				$new_name	=	date('ymdhis').".".$ext;
				$new_path	=	$pathinfo['dirname'].'/'.$new_name;
				rename($old_path, $new_path);
				$arr	=	array(
					'name'	=>	$data['upload_data']['client_name'],
					'type'	=>	$ext,
					'file_path'	=>	'uploads/'.$_SESSION['_user']['user_id'].'/'.$new_name,
					'created_by'	=>	$_SESSION['_user']['user_id'],
					'created_date'	=>	date('Y-m-d H:i:s'),
					'cat'		=>	$this->input->post('type'),
					'explanation'	=>	$explanation
				);
				$doc_id	=	$this->document_model->insert($arr);
				//$this->db->insert('documents_role',array('user_id' => $_SESSION['_user']['user_id'],'document_id' => $doc_id,'permission' => 'modify'));
				redirect(base_url().'document/');
			}
		}
		$this->load->view('document/upload', $this->data);
	}
	
	function cat($cat=null,$p=1)
	{
		$this->data['module_title']	=	($cat==1 ? 'Presentation Slides' : 'Statistics');
		$limit	=	5;
		$offset	=	($p-1)*$limit;	
		if($cat)$sql['cat']	=	$cat;
		$result	=	$this->document_model->get_my_documents($_SESSION['_user']['user_id'],$limit,$offset,$sql);
		$conf	=	array(
			'base_url'		=>	base_url().($cat==1 ? 'pres_slides' : 'statistics'),
			'cur_page'		=>	$p,	
			'total_rows'	=>	$result['num_rows'],
			'per_page'		=>	$limit
		);
		$this->pagination->initialize($conf);
		$this->data['pagnav']	=	$this->pagination->display();
		$this->data['documents']	=	$result['rows'];
		$this->load->view('document/cat',$this->data);
	}
	
	function view($id=null)
	{
		$document	=	$this->document_model->get_by('id',$id);
		if(!$document){show_error('File is not existed!');die();}
		$this->data['module_title']	=	'View document: '.$document->name;
		$this->data['document']	=	$document;
		$this->load->view('document/view',$this->data);
	}
	
	function download($id = null)
	{
		$this->load->helper('download');
		if($_SESSION['_user']['group_id'] == 1)
		{
			$doc	=	$this->document_model->get_by('id',$id);
		}else
		{
			//$doc	=	$this->document_model->get_by_user($_SESSION['_user']['user_id'],$id);
			$doc	=	$this->document_model->get_by('id',$id);
		}
		//print_r($_SESSION['_user']);
		//die();
		//print_r($doc);die;
		if(!$doc){show_error('File is not existed!');die();}
		$data	=	file_get_contents(base_url().$doc->file_path);
		$name	=	$doc->name.'.'.$doc->type;
		force_download($name, $data);
	}
	
	function edit($id = null)
	{
		$doc	=	$this->document_model->get_by('id',$id);
		$this->data['doc']	=	$doc;
		$this->load->view('document/edit', $this->data);
		if($_SERVER["REQUEST_METHOD"] == 'POST')
		{
			
			if($_FILES['file']['name'] != '')
			{
				$this->load->helper('upload');
				$path = dirname($_SERVER['SCRIPT_FILENAME']).'/uploads/';
				$path	.=	$_SESSION['_user']['user_id'];
				//echo $path;
				create_dir($path);
				$config['upload_path'] = $path;
				$config['allowed_types'] = 'doc|pdf|xls|docx|jpg|png|gif|jpeg|ico';
				//$name_file	=	$_FILES['']
				$explanation	=	$this->input->post('Explanation');
		
				$this->load->library('upload', $config);
				
		
				if (!$this->upload->do_upload('file'))
				{
					$error =$this->upload->display_errors();
					$this->data['error']	=	$error;
				}
				else
				{
					$data = array('upload_data' => $this->upload->data('file'));
					//print_r($data);die;
					//print_r($data);die;
					$old_path	=	$data["upload_data"]["full_path"];
					
					$pathinfo	=	pathinfo($old_path);
					$name		=	$pathinfo["filename"];
					$ext		=	$pathinfo["extension"];
					$new_name	=	date('ymdhis').".".$ext;
					$new_path	=	$pathinfo['dirname'].'/'.$new_name;
					rename($old_path, $new_path);
					$arr	=	array(
						'name'	=>	$data['upload_data']['client_name'],
						'type'	=>	$ext,
						'file_path'	=>	'uploads/'.$_SESSION['_user']['user_id'].'/'.$new_name,
						'cat'		=>	$this->input->post('type'),
						'explanation'	=>	$explanation
					);
					$doc_id	=	$this->document_model->update($id,$arr);
				}
			}//end co file
			else
			{
				$explanation	=	$this->input->post('Explanation');
				$arr	=	array(
					'cat'		=>	$this->input->post('type'),
					'explanation'	=>	$explanation
				);
				$doc_id	=	$this->document_model->update($id,$arr);
			}
			redirect(base_url().'document/');
		}
	}
	
	function del($id)
	{
		//
		$doc	=	$this->document_model->get_by('id',$id);
		if($doc){
			$this->document_model->delete($id);
			redirect(base_url().'document');
		}
	}
}